/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.opentripplanner.nedimporter;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;

/**
 *
 * @author demory
 */
public class NEDShapefileFeatureSource implements NEDFeatureSource {
  
  private FeatureCollection featureCollection;

  public NEDShapefileFeatureSource(String filename) {
    try {
      initFeatureCollection(new File(filename).toURI().toURL());
    } catch (MalformedURLException ex) {
      Logger.getLogger(NEDShapefileFeatureSource.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public void initFeatureCollection(URL shapeURL) {
    try {
      ShapefileDataStore store = new ShapefileDataStore(shapeURL);

      // feature type name is defaulted to the name of shapefile (without extension)
      String name = store.getTypeNames()[0];
      FeatureSource source = store.getFeatureSource(name);
      featureCollection = source.getFeatures();

      System.out.println("Feature collection type: " + featureCollection.getSchema().getName());
      System.out.println("Num features: " + featureCollection.size());
    } catch(IOException ex) {
      ex.printStackTrace();
    }
  }

  public FeatureCollection getFeatures() {
    return featureCollection;
  }

}
